<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="30%"
      @close="resetForm"
    >
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="120px" class="demo-ruleForm">
        <el-row>
          <el-col :span="24">
            <el-form-item label="设备名称" prop="device_name">
              <el-input v-model="ruleForm.device_name" size="mini" clearable placeholder="请输入设备名称" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="设备编号" prop="device_num">
              <el-input v-model="ruleForm.device_num" size="mini" clearable placeholder="请输入设备编号" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="站点" prop="site_id">
              <el-select
                v-model="ruleForm.site_id"
                style="width: 100%"
                size="mini"
                clearable
                filterable
                placeholder="请选择站点"
                @change="changeSite"
              >
                <el-option
                  v-for="item in optionsSite"
                  :key="item.value"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="分组" prop="device_group">
              <el-select v-model="ruleForm.device_group" style="width: 100%" size="mini" clearable filterable placeholder="请选择站点">
                <el-option
                  v-for="item in optionsGroup"
                  :key="item.value"
                  :label="item.group_name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="关联摄像" prop="camera_id">
              <el-select v-model="ruleForm.camera_id" style="width: 100%" size="mini" clearable filterable placeholder="请选择设备类型">
                <el-option
                  v-for="item in optionsCamera"
                  :key="item.value"
                  :label="item.device_name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="设备类型" prop="device_type">
              <el-select
                v-model="ruleForm.device_type"
                style="width: 100%"
                size="mini"
                clearable
                filterable
                placeholder="请选择设备类型"
                @change="changeType"
              >
                <el-option
                  v-for="item in optionsType"
                  :key="item.value"
                  :label="item.type_name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item v-if="optionsTarget.length > 0" label="指标" prop="target1">
              <el-checkbox-group
                v-model="ruleForm.target1"
                @change="change2"
              >
                <el-checkbox
                  v-for="item in optionsTarget"
                  :key="item.id"
                  :label="item.id"
                >{{ item.type_name }}</el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="是否参与计算" prop="calculate">
              <el-radio-group v-model="ruleForm.calculate">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item style="display: flex;justify-content: flex-end">
          <el-button type="primary" size="mini" @click="resetForm">取消</el-button>
          <el-button type="primary" size="mini" @click="addOrEdit">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  createCamera,
  detailCamera,
  getCameraAllList,
  getDictionary1,
  getFileAllList,
  getTarget,
  stationAllList,
  updateCamera
} from '@/api/fileManage'

export default {
  name: 'detail',
  props: {
    dialogVisible: {
      type: Boolean,
      default: true
    },
    title: {
      type: String,
      default: '新增'
    },
    id: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      ruleForm: {
        site_id: '', // 站点
        device_group: '', // 分组档案
        device_type: '', // 设备类型
        camera_id: '', // 关联摄像
        target1: [], // 指标组
        calculate: 1, // 是否参与计算 1:是, 2：否
        device_name: '', // 设备名称
        device_num: '' // 设备编号
      },
      rules: {
        site_id: [
          { required: true, message: '请选择站点', trigger: 'blur' }
        ],
        device_group: [
          { required: true, message: '请选择分组', trigger: 'blur' }
        ],
        device_type: [
          { required: true, message: '请选择设备类型', trigger: 'blur' }
        ],
        camera_id: [
          { required: true, message: '请选择关联摄像', trigger: 'blur' }
        ],
        target1: [
          { required: true, message: '请选择指标', trigger: 'blur' }
        ],
        calculate: [
          { required: true, message: '请选择是否参与计算', trigger: 'blur' }
        ],
        device_name: [
          { required: true, message: '请输入设备名称', trigger: 'blur' }
        ],
        device_num: [
          { required: true, message: '请输入设备编号', trigger: 'blur' }
        ]
      },
      optionsType: [], // 设备类型 --- 设备类型关联指标组
      optionsSite: [], // 站点列表 --- 站点关联分组档案和关联摄像
      optionsGroup: [], // 分组档案
      optionsCamera: [], // 关联摄像
      optionsTarget: [] // 关联指标组
    }
  },
  created() {
    this.getOptions()
    // 编辑
    if (this.id) {
      // this.ruleForm.pid = this.id
      if (this.title === '新增') {
        this.ruleForm.pid = this.id
      } else {
        this.getDetailById(this.id)
      }
    }
  },
  methods: {
    // 目标组选取
    change2(e) {
      console.log(e)
    },
    // 站点切换
    changeSite(e) {
      this.ruleForm.camera_id = ''
      this.ruleForm.device_group = ''
      // 获取分组、关联摄像
      this.getGroupAndCamera(e)
    },
    // 设备类型切换
    changeType(e) {
      // 获取指标组
      this.ruleForm.target = ''
      this.getTarget(e)
    },
    // 获取所有站点
    getOptions() {
      stationAllList().then(res => {
        this.optionsSite = res.data.data
      })
      getDictionary1({
        pid: 0,
        limit: 500,
        offset: 0
      }).then(res => {
        this.optionsType = res.data.categories
      })
    },
    // 获取指标组
    getTarget(id) {
      getTarget({
        pid: id
      }).then(res => {
        this.$set(this, 'optionsTarget', res.data.dataParam.categories)
        this.optionsTarget.forEach(item => {
          item.checked1 = false
        })
        for (let i = 0; i < this.optionsTarget.length; i++) {
          this.ruleForm.target1.forEach(item1 => {
            if (item1 === this.optionsTarget[i].id) {
              this.optionsTarget[i].checked1 = true
              console.log(this.optionsTarget[i])
            }
          })
        }
        this.$forceUpdate()
      })
    },
    // 获取分组、关联摄像
    getGroupAndCamera(id) {
      getFileAllList({ site_id: id }).then(res => {
        this.optionsGroup = res.data.list
      })
      getCameraAllList({ site_id: id }).then(res => {
        this.optionsCamera = res.data.videos
      })
    },
    // 获取详情
    getDetailById(id) {
      detailCamera({ id }).then(res => {
        this.ruleForm = { ...res.data, target1: [] }
        res.data.target_list.forEach(item => {
          this.ruleForm.target1.push(item.id)
        })
        this.getGroupAndCamera(this.ruleForm.site_id)
        this.getTarget(this.ruleForm.device_type)
      })
    },
    // 新增、编辑
    addOrEdit() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          this.ruleForm.target = this.ruleForm.target1
          delete this.ruleForm.target1
          if (this.title === '编辑') {
            //  是编辑
            updateCamera(this.ruleForm).then(res => {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.$emit('confirm')
            })
          } else {
            //  是新增
            createCamera(this.ruleForm).then(res => {
              this.$message({
                message: '新增成功',
                type: 'success'
              })
              this.$emit('confirm')
            })
          }
        }
      })
    },
    // 表单重置
    resetForm() {
      this.$refs['ruleForm'].resetFields()
      this.$emit('close')
    }
  }
}
</script>

<style scoped>

</style>
